package com.example.simplenet;

import java.util.Timer;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;

public class ArmyFragment extends MyFragment {

	private ImageButton mFieldArmy;
	private ImageButton mGarisonArmy;
	private ImageButton mTransportArmy;
	private ImageButton mSpyArmy;
	private ImageButton mMoveWood;
	private ImageButton mMoveStone;
	private ImageButton mMoveIron;
	private ImageButton mMoveAll;
	private CheckBox mRunAway;
	private CheckBox mAttackAbandoned;
	private Timer mAttackTimer;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.army_fragment, container, false);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {

		mFieldArmy = (ImageButton) getView().findViewById(R.id.fieldArmyButton);
		mGarisonArmy = (ImageButton) getView().findViewById(
				R.id.garnisonArmyButton);
		mTransportArmy = (ImageButton) getView().findViewById(
				R.id.transportArmyButton);
		mSpyArmy = (ImageButton) getView().findViewById(R.id.spyArmyButton);
		mMoveWood = (ImageButton) getView().findViewById(R.id.moveWoodButton);
		mMoveIron = (ImageButton) getView().findViewById(R.id.moveIronButton);
		mMoveStone = (ImageButton) getView().findViewById(R.id.moveStoneButton);
		mMoveAll = (ImageButton) getView().findViewById(R.id.moveAllButton);
		mRunAway = (CheckBox) getView().findViewById(R.id.runAwayCheckBox);
		mRunAway.setChecked(ArmyController.isRunAway());
		mAttackAbandoned = (CheckBox) getView().findViewById(
				R.id.attackAbandonedButton);
		mAttackAbandoned.setChecked(ArmyController.isAttackAbandoned());

		mFieldArmy.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Bundle bundle = new Bundle();
				bundle.putString("army", ConnectionMan.FIELD_ARMY);
				bundle.putBoolean("moveAll", false);
				((MainActivity)getActivity()).sendMessageToService(ConnectionMan.MSG_MOVE_ARMY, bundle);
			}
		});
		mGarisonArmy.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Bundle bundle = new Bundle();
				bundle.putString("army", ConnectionMan.GARNISON_ARMY);
				bundle.putBoolean("moveAll", false);
				((MainActivity)getActivity()).sendMessageToService(ConnectionMan.MSG_MOVE_ARMY, bundle);
			}
		});
		mTransportArmy.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Bundle bundle = new Bundle();
				bundle.putString("army", ConnectionMan.TRANSPORT_ARMY);
				bundle.putBoolean("moveAll", false);
				((MainActivity)getActivity()).sendMessageToService(ConnectionMan.MSG_MOVE_ARMY, bundle);
			}
		});
		mSpyArmy.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Bundle bundle = new Bundle();
				bundle.putString("army", ConnectionMan.SPY_ARMY);
				bundle.putBoolean("moveAll", false);
				((MainActivity)getActivity()).sendMessageToService(ConnectionMan.MSG_MOVE_ARMY, bundle);
			}
		});
		mMoveWood.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putString("resource", ConnectionMan.MoveResourcesThread.MOVE_WOOD);
				((MainActivity)getActivity()).sendMessageToService(ConnectionMan.MSG_MOVE_RESOURCES, bundle);
			}
		});
		mMoveIron.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putString("resource", ConnectionMan.MoveResourcesThread.MOVE_IRON);
				((MainActivity)getActivity()).sendMessageToService(ConnectionMan.MSG_MOVE_RESOURCES, bundle);
			}
		});
		mMoveStone.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putString("resource", ConnectionMan.MoveResourcesThread.MOVE_STONE);
				((MainActivity)getActivity()).sendMessageToService(ConnectionMan.MSG_MOVE_RESOURCES, bundle);
			}
		});
		mMoveAll.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putString("resource", ConnectionMan.MoveResourcesThread.MOVE_ALL);
				((MainActivity)getActivity()).sendMessageToService(ConnectionMan.MSG_MOVE_RESOURCES, bundle);
			}
		});
		mRunAway.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {

				ArmyController.setRunAway(isChecked);
			}
		});
		mAttackAbandoned.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Bundle bundle = new Bundle();
				bundle.putBoolean("turnOn", isChecked);
				((MainActivity)getActivity()).sendMessageToService(ConnectionMan.MSG_SPY_AND_ATTACK, bundle);
				ArmyController.setAttackAbandoned(isChecked);
			}
		});
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onDestroy() {

		if (mAttackTimer != null)
			mAttackTimer.cancel();
		super.onDestroy();
	}

}
